/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.nViron.Graffiti;
import java.util.StringTokenizer;

/**
 *
 * @author Roland
 */
public class LonLat {

	/**
	 * Default serialVersionUID.
	 */
	private static final long serialVersionUID = 1L;

        public LonLat(double _lng, double _lat) {
                lng = _lng;
                lat = _lat;
	}

	public LonLat(double _lng, double _lat, float _alt) {
                lng = _lng;
                lat = _lat;
                alt = _alt;
	}

	public double getLat() {
		return lat;
	}

	public double getLng() {
		return lng;
	}

        public double getAlt() {
		return alt;
	}

	@Override
	public int hashCode() {
		return (new Double(getLat()).hashCode() ^ new Double(getLng()).hashCode()) ^ new Float(getAlt()).hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		if (obj instanceof LonLat) {
			LonLat t = (LonLat) obj;
			return t.getLat() == getLat() && t.getLng() == getLng();
		}
		return false;
	}

         /**
	 * (37.34068368469045, -122.48519897460936)
	 */
	public static LonLat parse(String value) {
		try {
			StringTokenizer tokenizer = new StringTokenizer(value, "(, )");

			float lat = Float.valueOf(tokenizer.nextToken());
			float lng = Float.valueOf(tokenizer.nextToken());
			return new LonLat(lat, lng);
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * (lon=37.34068368469045, lat=-122.48519897460936)
	 */
	public static LonLat parseWithNames(String value) {
		try {
			StringTokenizer tokenizer = new StringTokenizer(value, "(, )");
			float lng = 0;
			float lat = 0;
			for (int i = 0; i < 2; i++) {
				String item = tokenizer.nextToken().toLowerCase();
				if (item.startsWith("lon=")) {
					lng = Float.parseFloat(item.substring(4));
				} else if (item.startsWith("lat=")) {
					lat = Float.parseFloat(item.substring(4));
				} else {
					return null;
				}
			}
			return new LonLat(lng, lat);
		} catch (Exception e) {
			return null;
		}
	}

        private double lng;
        private double lat;
        private float alt;
}